﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CommonMove : MonoBehaviour {

	// Use this for initialization
    private int m_iType,m_iDirection;
    private Vector3 m_vFrom, m_vTo, m_vMiddle,m_vEnding;
    private bool m_bIsMove,m_bIsShowFont,m_bIsChangeWindow,m_bIsChangeCameraSize;
    private float m_fCostTime, m_fDeltaTimeSum, m_fDeltaTimeFix, m_fStartFont, m_fEndFont,
        m_fFont, m_fV, m_fFrom, m_fTo, m_fChangeV,m_fCur,m_fMoveObjectLastDistance;
    private Text m_tFloor;
    private string m_sCallBackFunction;
    private int m_iCameraChangeDir;
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

        if (m_bIsChangeCameraSize)
        {
            if ((m_iCameraChangeDir == 1 && (m_fCur - m_fTo) < 0.1f) || (m_iCameraChangeDir == -1 && (m_fCur - m_fTo) > 0.1f))
            {
                m_fCur += m_fChangeV * Time.deltaTime * (m_fTo - m_fFrom);
                gameObject.GetComponent<Camera>().orthographicSize = m_fCur;
            }
            else
            {
                m_bIsChangeCameraSize = false;
            }
        }

        if (m_bIsChangeWindow)
        {
 
        }

        if (m_bIsShowFont)
        {
            //CommonFunctionA.logger(m_fFont, 1);
            if (m_fFont <= (m_fEndFont + (m_fEndFont - m_fStartFont)))
            {
                m_fFont += m_fV * Time.deltaTime;
                if (m_fFont >= m_fEndFont)
                {
                    m_tFloor.fontSize = (int)(2*m_fEndFont-m_fFont);
                }
                else
                {
                    m_tFloor.fontSize = (int)m_fFont;
                }
            }
            else
            {
                m_bIsShowFont = false;
            }
        }



        if (m_bIsMove)
        {
            switch (m_iType)
            {
                case 1:
                    //匀速移动
                    float fDist2 = Vector3.Distance(transform.position, m_vTo);
                    if (fDist2 > 0.1f && (fDist2 <= m_fMoveObjectLastDistance || m_fMoveObjectLastDistance == 0))
                    {
                        m_fMoveObjectLastDistance = fDist2;
                        transform.Translate(Time.deltaTime/m_fCostTime * (m_vTo - m_vFrom));
                    }
                    else
                    {
                        m_bIsMove = false;
                        //将其移到确切位置
                        transform.Translate(m_vTo - transform.position);
                        if (m_sCallBackFunction != null)
                        {
                            NotificationCenter.DefaultCenter().PostNotification(this, m_sCallBackFunction, null);
                        }
                    }
                    break;
                case 2:
                    //由慢转快，至中点再由快转慢
                    
                    float fDist=Vector3.Distance(transform.position, m_vEnding);
                    if (fDist > 1 && (fDist <= m_fMoveObjectLastDistance || m_fMoveObjectLastDistance==0))
                    {
                        m_fMoveObjectLastDistance = fDist;
                        if (m_iDirection == 1)
                        {
                            //起始时由慢至快
                            m_fDeltaTimeSum += Time.deltaTime;
                            if (m_fDeltaTimeSum >= 1.5f)
                            {
                                m_fDeltaTimeSum = 1.5f;
                                m_fDeltaTimeFix += Time.deltaTime;//说明中间匀速了这段时间。另一半回补
                            }
                            transform.Translate(m_fDeltaTimeSum * Time.deltaTime / m_fCostTime * (m_vEnding - m_vFrom));
                        }
                        else if (m_iDirection == -1)
                        {
                            //后半段由快至慢
                            m_fDeltaTimeFix -= Time.deltaTime;
                            if (m_fDeltaTimeFix < 0)
                            {
                                m_fDeltaTimeSum -= Time.deltaTime;
                                if (m_fDeltaTimeSum < 0.2f) m_fDeltaTimeSum = 0.2f;
                            }
                            transform.Translate(m_fDeltaTimeSum * Time.deltaTime / m_fCostTime * (m_vEnding - m_vMiddle));
                        }
                    }
                    else
                    {
                        if (m_iDirection == 1)
                        {
                            m_iDirection = -1;
                            m_vEnding = m_vTo;
                            m_fMoveObjectLastDistance = 0;
                        }
                        else if (m_iDirection == -1)
                        {
                            m_bIsMove = false;
                            if (m_sCallBackFunction != null)
                            {
                                NotificationCenter.DefaultCenter().PostNotification(this, m_sCallBackFunction, null);
                            }
                        }
                    }

                    break;
            }
        }
	}

    public void ChangeCameraSize(float fFrom, float fTo,float fChangeV)
    {
        m_fCur = m_fFrom = fFrom;
        m_fTo = fTo;
        m_fChangeV = fChangeV;
        if (fFrom <= fTo)
        {
            //说明是增大，方向设置为1
            m_iCameraChangeDir = 1;
        }
        else
        {
            //说明是减小，方向设置为-1
            m_iCameraChangeDir = -1;
        }
        m_bIsChangeCameraSize = true;
    }

    public void MoveObject(Vector3 vFrom, Vector3 vTo, int iType, float fCostTime,string sCallBackFunction=null)
    {
        m_iType = iType;
        m_vFrom = vFrom;
        m_fMoveObjectLastDistance = 0;
        if (iType == 2)
        {
            m_vEnding=m_vMiddle = (vFrom + vTo) / 2;//只有当使用第二种平移时调用
            m_iDirection = 1;
            m_fDeltaTimeFix = 0;
        }
        m_vTo = vTo;
        m_fCostTime = fCostTime;
        m_bIsMove = true;        
        m_sCallBackFunction = sCallBackFunction;
    }

    public void ShowTexture(float fStartFont,float fEndFont,float fV)
    {
        m_fStartFont = fStartFont;
        m_fEndFont = fEndFont;
        m_fFont = m_fStartFont;
        m_fV = fV;
        m_bIsShowFont=true;
        m_tFloor = transform.GetComponent<Text>();
    }

    public void ChangeWindow(float fStartFont, float fEndFont, float fV)
    {
        m_fStartFont = fStartFont;
        m_fEndFont = fEndFont;
        m_fFont = m_fStartFont;
        m_fV = fV;
        m_bIsShowFont = true;
        m_tFloor = transform.GetComponent<Text>();
    }
}
